Nginx Load Balancing: Simple Configuration for Multi-Server Traffic Distribution
This article introduces Nginx load balancing configuration to solve the problem of excessive load on a single server. At least two backend servers running the same service are required, with Nginx installed and the backend ports open. The core configuration consists of two steps: first, define the backend server group using `upstream` (supporting round-robin, weight, and health checks, e.g., `server 192.168.1.100:8080 weight=2;` or `max_fails=2 fail_timeout=10s`); second, configure `proxy_pass` to this group in the `server` block, passing the client's `Host` and real IP (`proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;`). Verification involves running `nginx -t` to check syntax, `nginx -s reload` to restart, and testing access to confirm request distribution. Common issues such as unresponsive backends or configuration errors can be resolved by checking firewalls and logs. Advanced strategies include IP hashing (`ip_hash`) and URL hashing (requires additional module).
Read MoreNginx Reverse Proxy: An Introduction to Load Balancing on Linux Servers
### Introduction to Nginx Reverse Proxy and Load Balancing **Core Functions**: Reverse proxy hides backend servers and unifies user access; load balancing distributes pressure across multiple servers to avoid single-point overload. **Reverse Proxy**: Similar to a "front desk receptionist," it receives user requests and forwards them to backend servers. Users need not know the specific backend servers, enhancing security and management efficiency. **Load Balancing**: When there are multiple backend servers, Nginx uses the `upstream` module to distribute requests. The default "round-robin" strategy can be adjusted as needed: - **Weighted Round-Robin**: Distributes requests by `weight` (e.g., `server 192.168.1.101 weight=5`); - **IP Hash**: Fixes user requests to a specific server (`ip_hash` directive). **Configuration Steps**: 1. Define backend server group: `upstream backend_servers { server 192.168.1.101; server 192.168.1.102; }`; 2. Configure reverse proxy: `proxy_pass http://backend_servers;` with `proxy_set_header` to forward request headers; 3. Test configuration.
Read More